"""
飞书配置设置
"""

import os
from typing import Optional
from dotenv import load_dotenv
from pydantic import Field, model_validator
from pydantic_settings import BaseSettings, SettingsConfigDict

load_dotenv()


class FeishuSettings(BaseSettings):
    """飞书应用配置"""
    
    # 飞书应用配置
    APP_ID: str = Field(default=os.getenv("FEISHU_APP_ID", ""), description="飞书应用ID")
    APP_SECRET: str = Field(default=os.getenv("FEISHU_APP_SECRET", ""), description="飞书应用密钥")
    VERIFICATION_TOKEN: str = Field(default=os.getenv("FEISHU_VERIFICATION_TOKEN", ""), description="飞书事件验证令牌")
    API_BASE_URL: str = Field(default=os.getenv("FEISHU_API_BASE_URL", ""), description="飞书API基础URL")
    
    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding="utf-8",
        case_sensitive=False,
        extra="ignore",
        env_prefix="FEISHU_"
    )


    @model_validator(mode='after')
    def validate_feishu_settings(self) -> 'FeishuSettings':
        """验证飞书配置"""
        if not self.APP_ID:
            raise ValueError("FEISHU_APP_ID不能为空，请在.env文件中设置")
        if not self.APP_SECRET:
            raise ValueError("FEISHU_APP_SECRET不能为空，请在.env文件中设置")
        if not self.VERIFICATION_TOKEN:
            raise ValueError("FEISHU_VERIFICATION_TOKEN不能为空，请在.env文件中设置")
        if self.API_BASE_URL and not self.API_BASE_URL.startswith(('http://', 'https://')):
            raise ValueError("API_BASE_URL必须以http://或https://开头")
        return self


# 全局飞书配置实例
feishu_settings = FeishuSettings()


def get_feishu_settings() -> FeishuSettings:
    """获取飞书配置实例"""
    return feishu_settings
